﻿
using Autofac.Extras.DynamicProxy;
using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Truron.Purchase.Interface;
using Truron.Purchase.Model;
using Truron.SysCenter.Model;
using Truron.Purchase.Service.DbContext;
using Truron.Utility;
using Truron.Utility.RedisHelper;

namespace Truron.Purchase.Service
{
	
	
	    
    [Intercept(typeof(CustomAutofacAop))]
    public class NResource_ExpressAddressService: INResource_ExpressAddressService
    {
        public NResource_ExpressAddressService()
        {
            
        }
       
        public NResource_ExpressAddress Save(NResource_ExpressAddress ent, UserCache user)
        {
             Database database = new Database();
             if (ent.Id == 0)
                    {
                        ent.Create(user);
                        var noteid = database.Insert(ent);
                        ent.Id = noteid;
                        return ent;
                    }
                    else
                    {
                        ent.Modify(user);
                        database.Update(ent);
                        return ent;
                    }
        }
        public NResource_ExpressAddress GetDetail(int Id)
        {
            return DapperDbContext.QueryFirstOrDefault<NResource_ExpressAddress>("select * from NResource_ExpressAddress where Status != @Status and Id=@Id", new { Status = Status.delete, Id = Id });
        }
        public int Del(List<int> Ids,UserCache user)
        {
            try
            {
                using (var conn = DapperHelper.Instance.OpenCurrentDbConnection())
                {
                    string sql = "UPDATE [dbo].[NResource_ExpressAddress] set [Status] = @Status,[LastUpdateUser] = @LastUpdateUser,[LastUpdateTime] = @LastUpdateTime where Id in @Id";
                    int delcount = conn.Execute(sql, new { Status = Status.delete, LastUpdateUser = user.UserId, LastUpdateTime = DateTime.Now, Id = Ids });
                    return delcount;
                }
                    
            }
            catch (Exception ex)
            {

                return -1;
            }
           
        }
        public List<NResource_ExpressAddress> GetListAll()
        {
            var List = DapperDbContext.Query<NResource_ExpressAddress>("select * from [NResource_ExpressAddress] where Status != @Status", new { Status = Status.delete }).ToList();
            return List;
        }
        public List<NResource_ExpressAddress> GetListByIds(List<int> ids)
        {
            string sql = " select * from NResource_ExpressAddress where Id in @Ids ";
            var List= DapperDbContext.Query<NResource_ExpressAddress>("select * from NResource_ExpressAddress where Id in @Ids and Status=0 ", new { Ids = ids }).ToList();
            return List;
        }
    }
}